#include "FrontLampNormalState.h"
#include "F3LampInterfaces.h"
#include "LampManager.h"
#include "LampModel.h"

FrontNormalStateMachine g_FrontNormalStateMachine;
void InitFrontNormalStateMachine(void)
{
    g_FrontNormalStateMachine.sm_.Entry = FrontNormalStateEntry;
    g_FrontNormalStateMachine.sm_.Excute = FrontNormalStateExcute;
    g_FrontNormalStateMachine.sm_.Exit = FrontNormalStateExit;
    g_FrontNormalStateMachine.sm_.waitExitCount_ = 0;
}

void FrontNormalStateEntry(void)
{
    // std::cout << "FrontNormalStateEntry" << std::endl;
}

void FrontNormalStateExcute(void)
{
    // std::cout << "FrontNormalStateExcute" << std::endl;
    //lobeam
    {
        g_LoBeamView.model_->onOffStatus_ = Get_CANSIG_LoBeamOnCmd();
        if(IsLampTurnOn(g_LoBeamView.model_->onOffStatus_))
        {
            g_LoBeamView.model_->pwm_ = LobeamDefaultPWM;
        }
        else
        {
            g_LoBeamView.model_->pwm_ = 0.0f;
        }

        g_Po2LedView.model_->onOffStatus_ = g_LoBeamView.model_->onOffStatus_;
    }

    //hibeam
    {
        g_HiBeamView.model_->onOffStatus_ = Get_CANSIG_HiBeamOnCmd();
    }

    //CornerLamp
    {
        bool AfsEn = Get_CANSIG_AFSEna() == AppAFSEna_Enable;
        bool speedOutofThreshold = Get_CANSIG_VehicleSpeed() > CornerLapmEnableVehicleSpeedThreshold;
        bool lobeamEnable = IsLampTurnOn(g_LoBeamView.model_->onOffStatus_);
        float steeringAngle = Get_CANSIG_SteeringAngle();
        bool SteeringAngleOutofThreshold = steeringAngle >= CornerLapmEnableSteeringAngleThresholdUpller || steeringAngle <= CornerLapmEnableSteeringAngleThresholdFloor || Is_TL_EN();

        g_CornerLampView.model_->onOffStatus_ = AfsEn && speedOutofThreshold && lobeamEnable && SteeringAngleOutofThreshold ? OnOff_On : OnOff_Off;
    }

    //DLP
    {
        g_DlpModelView.model_->isLM3409_EN_Enable_ = Is_IO_LM3409_EN();
        g_DlpModelView.model_->isDLP_LED1_And_DLP_LED2_Enable_ = (IsLampTurnOn(g_HiBeamView.model_->onOffStatus_) || IsLampTurnOn(g_LoBeamView.model_->onOffStatus_)) &&
                                                                 Get_CANSIG_UsageMode() != 0x04;
        g_DlpModelView.model_->DO_S32K_SL3409_PWM_ = Get_CANSIG_OdpLedBrtnsAdjstReq();
        g_DlpModelView.model_->angle_ = Get_CANSIG_PubProjectionAngleAdjstReq();

        if(g_DlpModelView.model_->angle_ != g_DlpModelView.lastModel_.angle_)
        {
            // if ProjectionAngle == level1 or if ProjectionAngle == level2, the DLP_LED1 and DLP_LED2 should turn off at once, but turn on in time time
            if(g_DlpModelView.model_->angle_ == ProjectionAngle_Level1 || g_DlpModelView.model_->angle_ == ProjectionAngle_Level2)
            {
                g_DlpModelView.model_->isDLP_LED1_And_DLP_LED2_Enable_ = false;
            }
        }
    }


    UpdateNormalState(g_FrontNewState.normalState_, &g_FrontLastState.normalState_);
}

void FrontNormalStateExit(void)
{
    // std::cout << "FrontNormalStateExit" << std::endl;
    UpdateNormalState(NormalStateLeFrntTurnLamp_Default, &g_FrontLastState.normalState_);
}


